Fix: Android model persistence and folder recognition - #2
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR targets two Android-specific bugs in the Flutter app: persisting the last-used ONNX model path across restarts and improving folder image discovery on newer Android versions where direct filesystem access is more restricted.
Changes:
- Added
shared_preferencesdependency and implemented saving/restoring the last model path. - Updated permission requests to include “all files access” flow (Android 11+) to improve external directory access.
- Tweaked batch directory listing to more explicitly filter file entities.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| android/pubspec.yaml | Adds shared_preferences for persisting model path. |
| android/lib/home_page.dart | Implements model path persistence, updates permission requests, and adjusts batch folder enumeration. |
| android/android/app/src/main/AndroidManifest.xml | Declares MANAGE_EXTERNAL_STORAGE permission for broader external storage access. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
54
to
+58
| Future<void> _requestPermissions() async { | ||
| // 申请存储权限(传统文件访问) | ||
| await Permission.storage.request(); | ||
| if (await Permission.photos.request().isDenied) { | ||
| // 低版本用 storage 已覆盖 | ||
| // 申请管理所有文件权限 (Android 11+),这能解决 Directory.listSync() 无法访问外部目录的问题 | ||
| if (await Permission.manageExternalStorage.request().isGranted) { |
Comment on lines
+63
to
+72
| Future<void> _loadSavedModel() async { | ||
| final prefs = await SharedPreferences.getInstance(); | ||
| final savedPath = prefs.getString('last_model_path'); | ||
| if (savedPath != null && File(savedPath).existsSync()) { | ||
| setState(() { | ||
| _modelPath = savedPath; | ||
| _log = '已恢复上次使用的模型'; | ||
| }); | ||
| } | ||
| } |
| _modelPath = path; | ||
| _log = '已选择本地模型: $path'; | ||
| }); | ||
| _saveModelPath(path); |
Comment on lines
133
to
+137
| setState(() { | ||
| _modelPath = savePath; | ||
| _log = '模型下载完成: $savePath'; | ||
| }); | ||
| _saveModelPath(savePath); |
Comment on lines
235
to
+239
| Future<void> _runBatch() async { | ||
| final files = Directory(_inputDir!) | ||
| .listSync() | ||
| .whereType<File>() | ||
| .listSync(recursive: false, followLinks: false) | ||
| .where((entity) => entity is File) | ||
| .cast<File>() |
Comment on lines
18
to
+23
| dio: ^4.0.6 | ||
| # 应用私有目录 | ||
| path_provider: ^2.0.15 | ||
| # 存储/相册权限 | ||
| permission_handler: ^9.2.0 | ||
| shared_preferences: ^2.0.15 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes two reported bugs in the Android version: